<?php

if ( defined( "_PARA__HELPERS" ) )
    return;
define("_PARA__HELPERS", 1 );


class Helpers {

    static function format_short_tstamp( $mysqltimestamp ) {
        if(preg_match('/(\d{2}):(\d{2}):(\d{2})$/', $mysqltimestamp, $matches) )
            return($matches[1] . ':' . $matches[2] . ':' . $matches[3] );
        return "HH:MM:SS";
    }

    // return date part of mysql timestamp //
    static function get_date( $mysqltimestamp ) {
        return substr( $mysqltimestamp, 0, 10 );
    }


    static function get_time( $mysqltimestamp ) {
        return substr( $mysqltimestamp, 11, 8 );
    }


    static function nice_date( $mysqltimestamp ) {
        global $parapara;
        $nowstamp   =  strtotime( self::get_date( $parapara->now)   . " 00:00:00" );
        $otherstamp =  strtotime( self::get_date( $mysqltimestamp ) . " 00:00:00" );

        $daydiff = floor( ( $nowstamp - $otherstamp ) / 86400 );

        $date = self::get_date( $mysqltimestamp );

        if( $daydiff == 0 )
            return "I dag d. $date";
        if( $daydiff == 1 )
            return "I går d. $date";

        return $date;
    }


    static function nice_timestamp( $mysqltimestamp ) {
        global $parapara;
        $nowstamp   =  strtotime( self::get_date( $parapara->now)   . " 00:00:00" );
        $otherstamp =  strtotime( self::get_date( $mysqltimestamp ) . " 00:00:00" );

        $daydiff = floor( ( $nowstamp - $otherstamp ) / 86400 );

        $date = self::get_date( $mysqltimestamp );
        $time = self::get_time( $mysqltimestamp );

        if( $daydiff == 0 )
            return "I dag kl. $time";
        if( $daydiff == 1 )
            return "I går kl. $time";

        return "$date kl. $time";

    }

    static function remove_magic_quotes( &$array ) {
        foreach (array_keys($array) as $key) {
            if (is_array($array[$key])) {
                Helpers::remove_magic_quotes($array[$key]);
            } else {
                $array[$key] = stripslashes($array[$key]);
            }
        }
    }


    static function validate_email( $email ) {
        if(!preg_match(
               '/^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/',
               $email) )
            return false;
        if(strlen( $email ) > 80 )
            return false;
        return true;
    }


    static function validate_screen_name( $screen_name ) {
        if(preg_match(
               '/^([a-zA-Z])([a-zA-Z \-_0-9\+]{2,49})$/',
               $screen_name ) )
            return true;
        return false;
    }


    static function validate_pwd( $pwd ) {
        if( (strlen($pwd) < 5) || (strlen($pwd) > 20) )
            return false;
        return true;
    }


    static function validate_url( $url ) {
        return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
    }



}

?>